import bpy
from bpy.types import Menu
from ..utils import QueueUtils


class QueueMenu(Menu, QueueUtils):
    bl_idname = 'QUEUE_MT_render'
    bl_label = '渲染队列菜单'

    def draw(self, context):
        from ..ops.queue import RenderStateSet, RenderSwitchFinished, RenderClearInvalidItem

        layout = self.layout
        layout.operator(RenderSwitchFinished.bl_idname)
        layout.operator(RenderClearInvalidItem.bl_idname)

        not_is_selected = not len(self.selected_queue)
        column = layout.column(align=True)
        column.enabled = not_is_selected
        column.label(text='将所选项设置为:')

        for (i, n, d) in self.render_state_items[:3]:
            column.operator(RenderStateSet.bl_idname, text=n).state = i

        if not_is_selected:
            col = layout.column()
            col.alert = not_is_selected
            layout.label(text="请选择至少一个渲染项以更改状态")


class_tuple = (
    QueueMenu,
)

register_class, unregister_class = bpy.utils.register_classes_factory(
    class_tuple)


def register():
    register_class()


def unregister():
    unregister_class()
    pref = QueueUtils.pref_()

    if pref and pref.active_render:
        pref.active_render.stop_render()
